package com.tanxi521.com.thenorthtitle.placeholder;

import com.tanxi521.com.thenorthtitle.TheNorthTitle;
import com.tanxi521.com.thenorthtitle.entity.TheNorthTitleData;
import com.tanxi521.com.thenorthtitle.sql.dao.TnTitleBaseInfoMapper;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;

public class TheNorthTitlePlaceHolder extends PlaceholderExpansion {

    public static final String start = "title";

    @Override
    public String onPlaceholderRequest(Player p, String params) {
        if (p != null && params.startsWith("player"))
            return getTitleHolder(p, params);
        return super.onPlaceholderRequest(p, params);
    }

    public static String getTitleHolder(Player p, String params) {
        TheNorthTitleData userTitle = TnTitleBaseInfoMapper.getUserTitle(p.getName());
        if (params.equals("player_name")) return null == userTitle.getTitleName() ? "未佩戴" : userTitle.getTitleName();
        if (params.equals("player_price"))
            return null == userTitle.getTitlePrice() ? "0" : userTitle.getTitlePrice().toString();
        if (params.equals("player_pre")) return null == userTitle.getTitleRelyOn() ? "无" : TnTitleBaseInfoMapper
                .selectTitleInfo(Integer.parseInt(userTitle.getTitleRelyOn())).getTitleName();
        if (params.equals("player_discount"))
            return null == userTitle.getTitleShopDiscount() ? "100%" : userTitle.getTitleShopDiscount() * 100 + "%";
        return "";
    }

    @Override
    public String getIdentifier() {
        return start;
    }

    @Override
    public String getAuthor() {
        return TheNorthTitle.instance.getDescription().getAuthors().toString();
    }

    @Override
    public String getVersion() {
        return TheNorthTitle.instance.getDescription().getVersion();
    }
}
